package com.tc.biz.provider;

import com.tc.activiti.util.BeanUtils;
import com.tc.biz.entity.BpmnApproval;
import com.tc.biz.entity.SysUser;
import com.tc.biz.service.BpmnApprovalService;
import com.tc.biz.service.IUserService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author zengqingfa
 * @date 2019/11/26 11:15
 * @description
 * @email zengqingfa_java@163.com
 */
@Component
public class BpmnBusinessProvider {

    @Autowired
    protected IUserService userService;
    @Autowired
    protected BpmnApprovalService bpmnApprovalService;

    /**
     * 查询各个审批节点的信息
     *
     * @param list
     * @return
     */
    public List<HashMap<String, Object>> getTaskDetail(List<HistoricTaskInstance> list) {
        List<HashMap<String, Object>> mapList = new ArrayList<>();
        if (BeanUtils.isNotEmpty(list)) {
            for (HistoricTaskInstance historicTaskInstance : list) {
                //只有结束时间不为空的任务才结束
                if(BeanUtils.isNotEmpty(historicTaskInstance.getEndTime())){
                    HashMap<String, Object> taskMap = new HashMap<>();
                    String id = historicTaskInstance.getId();
                    taskMap.put("taskId", id);
                    //办理人
                    SysUser assignee = userService.selectByPrimaryKey(new Long(historicTaskInstance.getAssignee()));
                    taskMap.put("historicTaskInstance", historicTaskInstance);
                    taskMap.put("assignee", assignee);
                    BpmnApproval bpmnApproval = bpmnApprovalService.findByTaskId(id);
                    if (BeanUtils.isNotEmpty(bpmnApproval)) {
                        taskMap.put("opinion", bpmnApproval.getOpinion());
                    }
                    mapList.add(taskMap);
                }
            }
        }
        return mapList;
    }
}
